package com.taptech.doufu.ui.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.weex.SimpleWeexActivity;
import com.taptech.doufu.constant.HomePageType;
import com.taptech.doufu.listener.IListScollerState;
import com.taptech.doufu.bean.cartoon.CartoonListBean;
import com.taptech.doufu.ui.view.AutoPollAdapter;
import com.taptech.doufu.ui.view.AutoPollRecyclerView;
import com.taptech.doufu.ui.view.viewflow.view.CircleFlowIndicator;
import com.taptech.doufu.ui.view.viewflow.view.HomeViewFlow;
import com.taptech.doufu.ui.view.TTHomeViewPager;
import com.taptech.doufu.util.CacheUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.ComicDayActivity;
import com.taptech.doufu.ui.activity.CartoonSortActivity;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.MarketNovelNoticeBean;
import com.taptech.doufu.bean.base.BaseResponseListBean;
import com.taptech.doufu.bean.cartoon.CartoonAdapterBean;
import com.taptech.doufu.bean.cartoon.CartoonBannerGson;
import com.taptech.doufu.bean.cp.ResponseCpIndexDataBean;
import com.taptech.doufu.listener.CommmonImp;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.ToUpOrDownable;
import com.taptech.doufu.ui.adapter.CartoonBeanAdapter;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.util.ScreenUtil;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.ui.view.HomeBannerImageAdapter;
import com.taptech.doufu.ui.view.PullToRefreshListView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * 漫画推荐
 */
public class CartoonRecommendFragment extends HomeBaseFragment implements PullToRefreshListView.OnLoadAndRefreshListener, View.OnClickListener {
    private static final String TAG = CartoonRecommendFragment.class.getSimpleName();
    private PullToRefreshListView listView;
    private HomeViewFlow viewFlow;
    private HomeBannerImageAdapter viewFlowAdapter;//轮播图适配器
    private CircleFlowIndicator flowIndicator;
    private LinearLayout llBrocatcast,llAutoLyaout,cartoonRecommendBgLayout;
    private List<DFHomeBannerBean> bannerList;
    private List<CartoonListBean> contentAdaList;
    private TTHomeViewPager viewPager;//父类所含有的viewpage，用来控制图片轮转时，触摸事件不回传给父类
    private CartoonBeanAdapter listAda;//滑动适配器
    private AutoPollRecyclerView autoPollRecyclerView;
    private List<MarketNovelNoticeBean> listNotice;
    private ImageView ivTop;

    //投稿
    private ImageView mCarttonPush;
    private LinearLayout dfhome_novbel_bg;
    private RelativeLayout dfhome_novel_line1;
    private RelativeLayout dfhome_novel_line2;
    private RelativeLayout dfhome_novel_line3;
    private ImageView dfhome_novel_iv1;
    private ImageView dfhome_novel_iv2;
    private ImageView dfhome_novel_iv3;
    private TextView dfhome_novel_tv1;
    private TextView dfhome_novel_tv2;
    private TextView dfhome_novel_tv3;

    private IListScollerState iListScollerState;
    private PullToRefreshListView.OnScrollYListener onScrollYListener;

    public void setOnScrollYListener(PullToRefreshListView.OnScrollYListener onScrollYListener) {
        this.onScrollYListener = onScrollYListener;
    }

    public void setiListScollerState(IListScollerState iListScollerState) {
        this.iListScollerState = iListScollerState;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dfcoterie_gay, null);
        getDefaultDisplay();
        initsView(rootView);
        return rootView;
    }


    private void loadRecommandDataCache() {
        Observable.create(new Observable.OnSubscribe<List<ResponseCpIndexDataBean.DataBean.ListBean>>() {
            @Override
            public void call(Subscriber<? super List<ResponseCpIndexDataBean.DataBean.ListBean>> subscriber) {
                JSONObject json = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_Cartoon_Recommand));
                List<ResponseCpIndexDataBean.DataBean.ListBean> list = null;
                try {
                    Gson gson = new Gson();
                    list = gson.fromJson(json.getJSONArray("list").toString(), new TypeToken<List<ResponseCpIndexDataBean.DataBean.ListBean>>() {}.getType());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                subscriber.onNext(list);
            }
        }).compose(RxJavaHelper.<List<ResponseCpIndexDataBean.DataBean.ListBean>>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<List<ResponseCpIndexDataBean.DataBean.ListBean>>(){
                    @Override
                    public void onNext(List<ResponseCpIndexDataBean.DataBean.ListBean> listBeen) {
                        super.onNext(listBeen);
                        setHotNovel(listBeen);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }

    private void initRxData() {
        ApiClient.getInstance().getService().getCartoonIndexList()
                .map(new Func1<ResponseCpIndexDataBean, List<ResponseCpIndexDataBean.DataBean.ListBean>>() {
                    @Override
                    public List<ResponseCpIndexDataBean.DataBean.ListBean> call(ResponseCpIndexDataBean responseCpIndexDataBean) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(responseCpIndexDataBean.getData()),new File(CommmonImp.Home_Cartoon_Recommand));
                        return responseCpIndexDataBean.getData().getList();
                    }
                })
                .compose(RxJavaHelper.<List<ResponseCpIndexDataBean.DataBean.ListBean>>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<List<ResponseCpIndexDataBean.DataBean.ListBean>>() {
                    @Override
                    public void onError(Throwable e) {
                        onRefreshComplete();
                    }
                    @Override
                    public void onNext(List<ResponseCpIndexDataBean.DataBean.ListBean> list) {
                        setHotNovel(list);
                    }
                });
    }




    /**
     * 热门作品等
     *
     * @param listBeans
     */
    private void setHotNovel(List<ResponseCpIndexDataBean.DataBean.ListBean> listBeans) {
        Observable.just(listBeans)
                .map(new Func1<List<ResponseCpIndexDataBean.DataBean.ListBean>, List<CartoonListBean>>() {
                    @Override
                    public List<CartoonListBean> call(List<ResponseCpIndexDataBean.DataBean.ListBean> listBean) {
                        List<CartoonAdapterBean> content1List = com.alibaba.fastjson.JSONArray.parseArray(new Gson().toJson(listBean), CartoonAdapterBean.class);
                        return changeData2(content1List);
                    }
                })
                .compose(RxJavaHelper.<List<CartoonListBean>>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<List<CartoonListBean>>(){
                    @Override
                    public void onNext(List<CartoonListBean> list) {
                        super.onNext(list);
                        contentAdaList = list;
                        if (listAda != null) {
                            listAda.refrushList(contentAdaList);
                        }else{
                            listAda = new CartoonBeanAdapter(getActivity(), contentAdaList);
                            listView.setAdapter(listAda);
                        }
                        onRefreshComplete();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        onRefreshComplete();
                    }
                });
    }


    /**
     * load cache
     */
    private void loadCacheDate() {
        //加载轮播图
        loadCartoonBannerCache();
        loadNovelSpeakerCache();
        loadRecommandDataCache();
    }


    private void initsView(View rootView) {
        listView = (PullToRefreshListView) rootView.findViewById(R.id.fragment_dfnovel_list);
        listView.setRefreshable(true);
        listView.setOnLoadAndRefreshListener(this);

        listView.setMoveListener(new ToUpOrDownable() {
            @Override
            public void moveToUp() {
                ivTop.setVisibility(View.INVISIBLE);
                setToTopVisible(false);
            }

            @Override
            public void moveToDown(int firstVisiblePos) {
                if (firstVisiblePos > 3) {
                    ivTop.setVisibility(View.VISIBLE);
                    setToTopVisible(true);
                }
            }
        });
        listView.setOnScrollStateListent(new PullToRefreshListView.IOnScrollState() {
            @Override
            public void onState(int state) {
                if (iListScollerState != null) {
                    iListScollerState.scrollState(state);
                }
            }
        });
        ivTop = (ImageView) rootView.findViewById(R.id.fragment_dfnovel_upTop);
        ivTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToTop();
            }
        });

        contentAdaList = new ArrayList<>();
        listAda = new CartoonBeanAdapter(getActivity(), contentAdaList);
        listView.setAdapter(listAda);
        listView.setOnScrollYListener(onScrollYListener);
        initListViewHeadFootView();
    }

    public void scrollToTop() {
        if (listView != null) {
            listView.setSelection(0);
        }
        if (ivTop != null) {
            ivTop.setVisibility(View.INVISIBLE);

        }
    }

    public void stopSpeaker() {
        if (autoPollRecyclerView != null) {
            autoPollRecyclerView.stop();
        }
    }

    public void startSpeaker() {
        if (autoPollRecyclerView != null) {
            autoPollRecyclerView.start();
        }
    }

    private boolean toTopButtonIsShow = false;

    private void setToTopVisible(boolean visibleAnimation) {
        ObjectAnimator objectAnimators = null;
        AnimatorSet animatorSet = new AnimatorSet();
        if (visibleAnimation) {
            //出现动画
            if (!toTopButtonIsShow) {
                objectAnimators = ObjectAnimator.ofFloat(ivTop, "translationY", ScreenUtil.getScreenHeight(this.getActivity()), 0);
            }
            toTopButtonIsShow = true;
        } else {
            //隐藏动画
            if (toTopButtonIsShow) {
                objectAnimators = ObjectAnimator.ofFloat(ivTop, "translationY", 0, ScreenUtil.getScreenHeight(this.getActivity()) - ivTop.getMeasuredHeight());
            }
            toTopButtonIsShow = false;
        }
        if (objectAnimators != null) {
            animatorSet.setDuration(500);
            animatorSet.playTogether(objectAnimators);
            animatorSet.start();
        }

    }

    private long lastUpdateTime = 0;

    /**
     * request server
     */
    private void getDataFromNet() {
        getHomeCartoonBanner();
        initRxData();
        if ((System.currentTimeMillis() - lastUpdateTime > 5000)) {
            if (listView.getLoadCompleteImg() != null) {
                listView.getLoadCompleteImg().setVisibility(View.GONE);
            }
            listView.setFull(false);
            listView.setLoadmoreable(true);
            getHomeNovelSpeaker();
            lastUpdateTime = System.currentTimeMillis();
        } else {
            onRefreshComplete();
        }
    }

    private void onRefreshComplete() {
        /**
         * 刷新动画做延迟处理
         */
        new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (listView != null) {
                                listView.onRefreshComplete();
                            }
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    /**
     * foot view and head view
     */
    private void initListViewHeadFootView() {
        View homeTopEnterView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_cartoon_recommend_view, null);
        dfhome_novbel_bg = (LinearLayout) homeTopEnterView.findViewById(R.id.dfhome_novbel_bg);
        dfhome_novbel_bg.setVisibility(View.VISIBLE);
        dfhome_novel_line1 = (RelativeLayout) homeTopEnterView.findViewById(R.id.dfhome_novel_line1);
        dfhome_novel_line1.setOnClickListener(this);
        dfhome_novel_line2 = (RelativeLayout) homeTopEnterView.findViewById(R.id.dfhome_novel_line2);
        dfhome_novel_line2.setOnClickListener(this);
        dfhome_novel_line3 = (RelativeLayout) homeTopEnterView.findViewById(R.id.dfhome_novel_line3);
        dfhome_novel_line3.setOnClickListener(this);
        dfhome_novel_iv1 = (ImageView) homeTopEnterView.findViewById(R.id.dfhome_novel_iv1);
        dfhome_novel_iv2 = (ImageView) homeTopEnterView.findViewById(R.id.dfhome_novel_iv2);
        dfhome_novel_iv3 = (ImageView) homeTopEnterView.findViewById(R.id.dfhome_novel_iv3);
        dfhome_novel_tv1 = (TextView) homeTopEnterView.findViewById(R.id.dfhome_novel_tv1);
        dfhome_novel_tv2 = (TextView) homeTopEnterView.findViewById(R.id.dfhome_novel_tv2);
        dfhome_novel_tv3 = (TextView) homeTopEnterView.findViewById(R.id.dfhome_novel_tv3);

        llBrocatcast = (LinearLayout) homeTopEnterView.findViewById(R.id.ll_dfhome_viewflow_broatcast);
        llAutoLyaout = (LinearLayout) homeTopEnterView.findViewById(R.id.fg_cartoon_recommend_notice_layout);
        cartoonRecommendBgLayout=  (LinearLayout) homeTopEnterView.findViewById(R.id.cartoon_recommend_bg);
        viewFlow = (HomeViewFlow) homeTopEnterView.findViewById(R.id.dfhome_exchange_viewflow);
        viewFlow.setLayoutParams(new RelativeLayout.LayoutParams(ScreenUtil.getScreenWidth(getActivity()), (int) (ScreenUtil.getScreenWidth(getActivity())/1.5)));
        flowIndicator = (CircleFlowIndicator) homeTopEnterView.findViewById(R.id.dfhome_ad_indicator);
        flowIndicator.setVisibility(View.VISIBLE);
        autoPollRecyclerView =  homeTopEnterView.findViewById(R.id.autoPollRecyclerView);
        listView.addHeaderView(homeTopEnterView);


    }

    @Override
    public synchronized void onRefresh() {
        listView.setLoadmoreable(false);
        //重新加载数据
        getDataFromNet();
    }


    @Override
    public synchronized void loadMore() {
        listView.loadMoreComplete();
        listView.setLoadmoreable(true);
        return;
    }


    /**
     *
     */
    private void loadNovelSpeakerCache() {
        Observable.create(new Observable.OnSubscribe<List<MarketNovelNoticeBean>>() {
            @Override
            public void call(Subscriber<? super List<MarketNovelNoticeBean>> subscriber) {
                JSONObject json = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_Cartoon_Market));
                List<MarketNovelNoticeBean> list = null;
                try {
                    list = com.alibaba.fastjson.JSONArray.parseArray(json.getJSONArray("list").toString(), MarketNovelNoticeBean.class);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                subscriber.onNext(list);
            }
        })
        .compose(RxJavaHelper.<List<MarketNovelNoticeBean>>observeOnMainThread(getActivity()))
        .subscribe(new BaseSubscriber<List<MarketNovelNoticeBean>>(){
            @Override
            public void onNext(List<MarketNovelNoticeBean> marketNovelNoticeBeen) {
                super.onNext(marketNovelNoticeBeen);
                listNotice = marketNovelNoticeBeen;
                if (listNotice != null && listNotice.size() > 0) {
                    if (!TextUtils.isEmpty(listNotice.get(0).getMsg())) {
                        llBrocatcast.setVisibility(View.VISIBLE);
                        AutoPollAdapter adapter = new AutoPollAdapter(getActivity(), listNotice,HomePageType.COMIC);
                        autoPollRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                        autoPollRecyclerView.setAdapter(adapter);
                        autoPollRecyclerView.start();
                    } else {
                        llBrocatcast.setVisibility(View.GONE);
                    }
                } else {
                    llBrocatcast.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    /**
     * 获取小喇叭数据
     */
    public void getHomeNovelSpeaker() {
        ApiClient.getInstance().setUseCache(false).setSexType(ApiClient.SEX_TYPE_NO).getService().getNovelSpeaker(ApiClient.SEX_TYPE_NO)
                .map(new Func1<BaseResponseListBean<MarketNovelNoticeBean>, List<MarketNovelNoticeBean>>() {
                    @Override
                    public List<MarketNovelNoticeBean> call(BaseResponseListBean<MarketNovelNoticeBean> speakerData) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(speakerData.getData()),new File(CommmonImp.Home_Cartoon_Market));
                        JsonArray topicData = new Gson().toJsonTree(speakerData.getData().getList()).getAsJsonArray();
                        return com.alibaba.fastjson.JSONArray.parseArray(topicData.toString(), MarketNovelNoticeBean.class);
                    }
                })
                .compose(RxJavaHelper.<List<MarketNovelNoticeBean>>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<List<MarketNovelNoticeBean>>() {
                    @Override
                    public void onNext(List<MarketNovelNoticeBean> speakerData) {
                        super.onNext(speakerData);
                        listNotice = speakerData;
                        if (listNotice != null && listNotice.size() > 0) {
                            if (!TextUtils.isEmpty(listNotice.get(0).getMsg())) {
                                llBrocatcast.setVisibility(View.VISIBLE);
                                AutoPollAdapter adapter = new AutoPollAdapter(getActivity(), listNotice,HomePageType.COMIC);
                                autoPollRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
                                autoPollRecyclerView.setAdapter(adapter);
                                autoPollRecyclerView.start();
                            } else {
                                llBrocatcast.setVisibility(View.GONE);
                            }
                        } else {
                            llBrocatcast.setVisibility(View.GONE);
                        }
                    }
                });
    }

    private void loadCartoonBannerCache() {
        Observable.create(new Observable.OnSubscribe<List<DFHomeBannerBean>>() {
            @Override
            public void call(Subscriber<? super List<DFHomeBannerBean>> subscriber) {
                JSONObject data = CacheUtil.readJsonDataFromCache(new File(CommmonImp.Home_Cartoon_Banner));
                List<DFHomeBannerBean> list = null;
                try {
                     list = com.alibaba.fastjson.JSONArray.parseArray(data.getJSONArray("data").toString(), DFHomeBannerBean.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                subscriber.onNext(list);
            }
        }).compose(RxJavaHelper.<List<DFHomeBannerBean>>observeOnMainThread(getActivity()))
        .subscribe(new BaseSubscriber<List<DFHomeBannerBean>>(){
            @Override
            public void onNext(List<DFHomeBannerBean> list) {
                super.onNext(list);
                if (list != null) {
                    bannerList = list;
                    initFlowData();
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }

    private void getHomeCartoonBanner() {
        ApiClient.getInstance().getService().getCartoonBanner("comics.recommend")
                .map(new Func1<CartoonBannerGson, CartoonBannerGson>() {
                    @Override
                    public CartoonBannerGson call(CartoonBannerGson cartoonBannerGson) {
                        CacheUtil.saveJsonDataToCache(new Gson().toJson(cartoonBannerGson).toString(), new File(CommmonImp.Home_Cartoon_Banner));
                        return cartoonBannerGson;
                    }
                })
                .compose(RxJavaHelper.<CartoonBannerGson>observeOnMainThread(getActivity()))
                .subscribe(new BaseSubscriber<CartoonBannerGson>(){
                    @Override
                    public void onNext(CartoonBannerGson bannerBaseResponseBean) {
                        super.onNext(bannerBaseResponseBean);
                        if (bannerBaseResponseBean != null && bannerBaseResponseBean.getData()!=null) {
                            bannerList = bannerBaseResponseBean.getData();
                            if (bannerList != null) {
                                initFlowData();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });

    }



    private synchronized List<CartoonListBean> changeData2(List<CartoonAdapterBean> tempList) {
        List<CartoonListBean> contentAdaList = new ArrayList<>();
        try {
            if ((tempList != null) && (tempList.size() > 0)) {
                for (int i = 0; i < tempList.size(); i++) {
                    if ((tempList.get(i).getList() != null) && (tempList.get(i).getList().size() > 0)) {
                        CartoonListBean bean = new CartoonListBean();
                        bean.setModel(tempList.get(i).getModel());
                        bean.setMoreUrl(tempList.get(i).getMore_url());
                        bean.setTitle(tempList.get(i).getTitle());
                        bean.setList(tempList.get(i).getList());
                        contentAdaList.add(bean);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return contentAdaList;
        }
    }



    private void initFlowData() {

        try {
            if (viewFlow != null && bannerList != null) {
                viewFlowAdapter = new HomeBannerImageAdapter(bannerList, getActivity(),HomePageType.COMIC);
                viewFlow.setAdapter(viewFlowAdapter);
                viewFlow.setmSideBuffer(bannerList.size());// 实际图片张数为size大小
                viewFlow.setTimeSpan(5000);// 图片切换时间间隔
                viewFlow.setSelection(3 * 1000); // 设置初始位置
                if (viewPager != null) {
                    viewFlow.setViewPage(viewPager);
                }
                flowIndicator.setViewFlow(viewFlow);
                viewFlow.setFlowIndicator(flowIndicator);
                if (bannerList.size() > 1) {
                    viewFlow.startAutoFlowTimer(); // 启动自动播放
                }
            } else {
                viewFlowAdapter.setData(bannerList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setViewPager(TTHomeViewPager mViewPager) {
        this.viewPager = mViewPager;
    }


    private void getDefaultDisplay() {
        DisplayMetrics dpMetrics = new DisplayMetrics();
        getActivity().getWindow().getWindowManager().getDefaultDisplay()
                .getMetrics(dpMetrics);
    }


    /**
     * ui onclick
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dfhome_novel_line1: //分类
                TMAnalysis.event(getActivity(), "MAIN_CARTOON_CATEGORY");
                Intent intent1 = new Intent(getActivity(), CartoonSortActivity.class);
                startActivity(intent1);
                break;
            case R.id.dfhome_novel_line2://日更
                TMAnalysis.event(getActivity(), "MAIN_CARTOON_DAY_UPDATE");
                Intent intent2 = new Intent(getActivity(), ComicDayActivity.class);
                startActivity(intent2);
                break;
            case R.id.dfhome_novel_line3://排行
                TMAnalysis.event(getActivity(), "MAIN_CARTOON_RANK");
                SimpleWeexActivity.startActivity(getActivity(), "vue/comicRank/comicRankPage.js");
                break;
        }
    }

    /**
     * 我要投稿
     */
    private void startPushActivity() {
        TMAnalysis.event(getActivity(), "3_7_PersonalCenter_mymember");
        Intent intent = new Intent();
        intent.setClass(getActivity(), BrowseActivity.class);
        intent.putExtra(Constant.URL, "http://api.doufu.la/index.php/comics/deliver_comics");
        startActivity(intent);
    }


    private boolean isAttachActivity = false;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        isAttachActivity = true;
    }

    public void changeThemeWithColorful() {
        if (!isAttachActivity) {
            return;
        }
        if (WeMediaApplication.getInstance().isDayNightMode_Night) {
            changeDarkViewBg();
        } else {
            changeDayViewBg();
        }
        if(listAda != null){
            listAda.notifyDataSetChanged();
        }
    }

    private void changeDayViewBg(){
        try {
            mCarttonPush.setAlpha(1.0f);
            llBrocatcast.setBackgroundColor(getResources().getColor(R.color.white));
            llAutoLyaout.setBackground(getResources().getDrawable(R.drawable.shape_roundrectangle));
            cartoonRecommendBgLayout.setBackgroundColor(getResources().getColor(R.color.all_activity_bg));
            dfhome_novbel_bg.setBackgroundColor(getResources().getColor(R.color.white));
            dfhome_novel_tv1.setTextColor(getResources().getColor(R.color.text_color_2));
            dfhome_novel_tv2.setTextColor(getResources().getColor(R.color.text_color_2));
            dfhome_novel_tv3.setTextColor(getResources().getColor(R.color.text_color_2));
            dfhome_novel_iv1.setAlpha(1.0f);
            dfhome_novel_iv2.setAlpha(1.0f);
            dfhome_novel_iv3.setAlpha(1.0f);
        } catch (NullPointerException e) {
        }
    }
    private void changeDarkViewBg(){
        try {
            mCarttonPush.setAlpha(Constant.imagViewAlpha);
            llBrocatcast.setBackground(getResources().getDrawable(R.color.fg_dark));
            llAutoLyaout.setBackground(getResources().getDrawable(R.drawable.shape_roundrectangle_dark));
            cartoonRecommendBgLayout.setBackgroundColor(getResources().getColor(R.color.bg_dark));
            dfhome_novbel_bg.setBackgroundColor(getResources().getColor(R.color.fg_dark));
            dfhome_novel_tv1.setTextColor(getResources().getColor(R.color.text_dark));
            dfhome_novel_tv2.setTextColor(getResources().getColor(R.color.text_dark));
            dfhome_novel_tv3.setTextColor(getResources().getColor(R.color.text_dark));
            dfhome_novel_iv1.setAlpha(Constant.imagViewAlpha);
            dfhome_novel_iv2.setAlpha(Constant.imagViewAlpha);
            dfhome_novel_iv3.setAlpha(Constant.imagViewAlpha);
        } catch (NullPointerException e) {

        }
    }

    @Override
    public void fetchData() {
        loadCacheDate();
        getDataFromNet();
    }
}
